import { Pipe, PipeTransform, Inject } from "@angular/core";

@Pipe({
  name: "thousands"
})
export class ThousandsPipe implements PipeTransform {
  constructor(@Inject("ConfigService") private configService) {}
  transform(value: any, args?: any): any {
    const arr = String(value).split(".");
    const n = arr[0];
    const p = arr[1] ? arr[1] : "";

    if (n.length <= 3) {
      return value;
    }

    const nr = n.split("").reverse();

    const r = [];

    for (let i = 0; i < nr.length; i++) {
      const item = nr[i];
      const j = i + 1;
      if (j === 4) {
        r.push(",");
      }
      if (j > 4 && i % 3 === 0) {
        r.push(",");
      }
      r.push(item);
    }

    return `${r.reverse().join("")}${p.length ? "." : ""}${p}`;
  }
}
